<template>
  <view>
    <picker 
      mode="selector" 
      :range="pickerArray" 
      :value="selectedIndex" 
      @change="pickerChange">
      <view>
        {{ pickerArray[selectedIndex] }}
      </view>
    </picker>

    <view>选择的值：{{ selectedValue }}</view>
    <view>
    </view>
    <view>
      <text>定义请求参数：</text>
    </view>
    <view>
      <textarea
        v-model="requestParam"
        placeholder="请求参数"
        style="height: 100rpx;border: 1px solid #666666;"
        placeholder-class="textarea-placeholder"
        @input=""
      />
      <text @click="doRequest">发送请求</text>
    </view>
    <view></view>
    <view><button
      :disabled="false"
      :loading="false"
      open-type=""
      hover-class="button-hover"
      @click="doSubscribe"
    >
      订阅通知
    </button></view>
    <view></view>
    <view>
      <text>响应结果</text>
    </view>
    <view>
      <textarea
        v-model="responseBody"
        placeholder="响应结果"
        style="border: 1px solid #666666;"
        placeholder-class="textarea-placeholder"
        @input=""
      />
    </view>
  </view>
</template>

<script>
import { Api } from '../../api/api';

export default {
  components: {
    
  },
  mixins: [],
  data() {
    return {
      pickerArray: ['选项1', '选项2', '选项3', '选项4'],
      selectedIndex: 0,
      selectedValue: '',
      requestParam: '',
      responseBody: '',
    }
  },
  computed: {},
  methods: {
    pickerChange(e) {
      const index = e.detail.value;
      this.selectedIndex = index;
      this.selectedValue = this.pickerArray[index];
    },
    doRequest(){
      let that = this
      let params = JSON.parse(this.requestParam)
      console.log('请求参数params', params)
      let apis = this.selectedValue.split('-')
      console.log('请求api信息', this.selectedValue)
      Api[apis[0]][apis[1]](params).then(res => {
        console.log('接口响应结果', res)
        that.responseBody = res
      })
    },
    doSubscribe(){
      Api.CommentApi.add({}, true)
    }
  },
  watch: {},

  // 页面周期函数--监听页面加载
  onLoad() {
    const keys = Object.keys(Api);
    let tempAry = []
    keys.forEach(key => {
      const subKeys = Object.keys(Api[key])
      subKeys.forEach(subKey => {
        tempAry.push(key + "-" + subKey)
      })
    })
    this.pickerArray = tempAry
    
    // for(const key in keys){
    //   console.log('sdfsdfsd', key)
    //   console.log('sdfsdf', Api[key])
    //   subKeys = Object.keys(Api[key])
    //   for(const subKey in subKeys){
    //     this.pickerArray.push(key + "-" + subKey)
    //   }
    // }
    // this.pickerArray = keys
    // console.log('options', this.secondPickerOptions)
  },
  // 页面周期函数--监听页面初次渲染完成
  onReady() {},
  // 页面周期函数--监听页面显示(not-nvue)
  onShow() {},
  // 页面周期函数--监听页面隐藏
  onHide() {},
  // 页面周期函数--监听页面卸载
  onUnload() {},
  // 页面处理函数--监听用户下拉动作
  // onPullDownRefresh() { uni.stopPullDownRefresh(); },
  // 页面处理函数--监听用户上拉触底
  // onReachBottom() {},
  // 页面处理函数--监听页面滚动(not-nvue)
  // onPageScroll(event) {},
  // 页面处理函数--用户点击右上角分享
  // onShareAppMessage(options) {},
}
</script>

<style scoped></style>